package com.cyt.sys.service.impl;

import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.BaseUtil;
import com.cyt.base.util.ErrorCode;
import com.cyt.base.util.StringUtils;
import com.cyt.sys.dao.SysOrganizationDao;
import com.cyt.sys.model.po.SysOrganization;
import com.cyt.sys.model.vo.SysOrganizationVo;
import com.cyt.sys.service.SysOrganizationService;
import com.github.pagehelper.StringUtil;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 *@Author: liufu zhu
 *@Date: 2018-11-30 14:48
 *@Description:基础部分组织机构service实现类
 */
@Service("baseOrganizationService")
public class SysOrganizationServiceImpl extends BaseServiceImpl<SysOrganization> implements SysOrganizationService {

    @Resource
    private SysOrganizationDao sysOrganizationDao;

    @Autowired
    public void setBaseDao(SysOrganizationDao sysOrganizationDao) {
        this.baseDao = sysOrganizationDao;
    }

    /**
     * @auther: liufu zhu
     * @create: 2018/11/30 15:35
     * @Description:  组织机构list查询
     */
    @Override
    public ReturnResult list(SysOrganizationVo sysOrganizationVo) {
        return new ReturnResult(listExample(sysOrganizationVo));
    }

    /**
     * @auther: liufu zhu
     * @create: 2018/11/30 17:49
     * @Description:  组织机构删除
     */
    @Override
    public ReturnResult delete(String ids) {
        String[] idList=ids.split(",");
        boolean flage=true;
        for (String id:idList){
            int num= sysOrganizationDao.deleteByPrimaryKey(id);
        }
        if(flage){
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_DEL);
        }else{
            return new ReturnResult(ErrorCode.CODE_101_MSG);
        }
    }

    /**
     * @auther: liufu zhu
     * @create: 2018/12/1 8:22
     * @Description:  编辑页面跳转
     */
    @Override
    public SysOrganization selectById(String id) {
        SysOrganization sysOrganization = sysOrganizationDao.selectById(id);
        return sysOrganization;
    }

    /**
     *@Author: YuShengQiang
     *@Date: 2018-12-05 16:16
     *@Description:获取组织机构树
     */
    @Override
    public ReturnResult dTreeList(SysOrganizationVo sysOrganizationVo) {
        List<SysOrganization> list = listExample(sysOrganizationVo);
        List<Map<String,Object>> dtreeEntityList=new ArrayList<Map<String,Object>>();
        for(SysOrganization sysOrganization :list){
            Map<String,Object> dtreeEntity=new HashedMap();
            dtreeEntity.put("id", sysOrganization.getId());
            dtreeEntity.put("title", sysOrganization.getName());
            dtreeEntity.put("parentId", StringUtils.isEmpty(sysOrganization.getParentId())? "0": sysOrganization.getParentId());
            dtreeEntity.put("isLast",isLast(list, sysOrganization.getId()));
            Map<String,String> m=new HashedMap();
            m.put("type","0");
            m.put("isChecked","0");
            List<Map<String,String>> dtreeCheckEntityList=new ArrayList<Map<String,String>>();
            dtreeCheckEntityList.add(m);
            dtreeEntity.put("checkArr",dtreeCheckEntityList);
            dtreeEntityList.add(dtreeEntity);
        }
        return new ReturnResult(dtreeEntityList);
    }

    /**
     *@Author: YuShengQiang
     *@Date: 2018-12-05 17:44
     *@Description:判断当时对象是否有子对象
     */
    private boolean isLast(List<SysOrganization> list, String id){
        if(list!=null&&list.size()>0&&StringUtil.isNotEmpty(id)){
            for(SysOrganization sysOrganization :list){
                if(id.equals(sysOrganization.getParentId())){
                    return false;
                }
            }
        }
        return true;
    }
    /**
     * @auther: liufu zhu
     * @create: 2018/12/1 9:12
     * @Description:  保存/更新组织机构
     */
    @Override
    public ReturnResult addOrUpdate(SysOrganizationVo sysOrganizationVo) {
        if (sysOrganizationVo.getId() == null || "".equals(sysOrganizationVo.getId())) {
            SysOrganization sysOrganization = new SysOrganization();
            sysOrganization.setId(BaseUtil.uId());
            sysOrganization.setParentId(sysOrganizationVo.getParentId());
            sysOrganization.setName(sysOrganizationVo.getName());
            sysOrganization.setSort(sysOrganizationVo.getSort());
            sysOrganization.setCode(sysOrganizationVo.getCode());
            sysOrganization.setStatus(sysOrganizationVo.getStatus());
            sysOrganization.setCreateTime(new Date());
            int id = sysOrganizationDao.insert(sysOrganization);
            if (id <= 0) {
                return new ReturnResult(BaseConstant.DATABASE_ERROR_INSERT);
            }
        } else {
            SysOrganization sysOrganization = sysOrganizationDao.selectByPrimaryKey(sysOrganizationVo.getId());
            sysOrganization.setName(sysOrganizationVo.getName());
            sysOrganization.setSort(sysOrganizationVo.getSort());
            sysOrganization.setCode(sysOrganizationVo.getCode());
            sysOrganization.setStatus(sysOrganizationVo.getStatus());
            int id = sysOrganizationDao.updateByPrimaryKey(sysOrganization);
            if (id <= 0) {
                return new ReturnResult(BaseConstant.DATABASE_ERROR_UPDATE);
            }
        }
        return new ReturnResult(BaseConstant.DATABASE_SUCCEED_MSG);
    }

    /**
     *@Author: YuShengQiang
     *@Date: 2018-12-05 15:54
     *@Description:查询对象集合
     */
    public List<SysOrganization> listExample(SysOrganizationVo sysOrganizationVo) {
        Example example = new Example(SysOrganization.class);
        Example.Criteria criteria = example.createCriteria();
        example.setOrderByClause(" create_time desc");
        if (StringUtil.isNotEmpty(sysOrganizationVo.getName())) {
            criteria.andLike("name", "%" + sysOrganizationVo.getName() + "%");
        }
        if(StringUtil.isNotEmpty(sysOrganizationVo.getStatus())){
            criteria.andEqualTo("status", sysOrganizationVo.getStatus());
        }
        List<SysOrganization> list = sysOrganizationDao.selectByExample(example);
        return list;
    }
}